package com.itheima.controller;


import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.Role;
import com.itheima.pojo.User;
import com.itheima.service.UserMessageService;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

    //用户管理
    @RequestMapping("/userMessage")
    @RestController
    public class UserMessageContorller {

    @Reference
    private UserMessageService userMessageService;

    @RequestMapping("/findUserMessage")
    public Result findUserMessage(@RequestBody QueryPageBean queryPageBean){
        try {
            PageResult  pageResult= userMessageService.findUserMessage(queryPageBean);
            return new Result(true, MessageConstant.QUERY_SETMEAL_SUCCESS,pageResult);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,MessageConstant.QUERY_SETMEAL_FAIL);
        }
    }

    //回显角色信息
    @RequestMapping("/findRoleAll")
    public Result findRoleAll(){
        try {
            List<Role> roles = userMessageService.findRoleAll();
            //返回信息需要修改
            return new Result(true,MessageConstant.QUERY_SETMEAL_SUCCESS,roles);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.QUERY_SETMEAL_FAIL);
        }
    }

    //新建添加数据
    @RequestMapping("/addUserAndRoles")
    public Result addUserAndRoles(@RequestBody User user,Integer[] checkitemIds){

        try {
            userMessageService.addUserAndRoles(user,checkitemIds);
            return new Result(true,"创建新用户成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"创建新用户失败");
        }

    }

    //编辑回显基本信息
    @RequestMapping("/findUser")
    public Result findUser(Integer userId){
        try {
           User user = userMessageService.findUser(userId);
            return new Result(true,MessageConstant.QUERY_ORDER_SUCCESS,user);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.QUERY_SETMEAL_FAIL);
        }
    }

    //编辑回显角色权限信息
    @RequestMapping("/findAndRole")
    public Result findAndRole(Integer userId){

        try {
         List<Integer> rolesId = userMessageService.findAndRole(userId);
            return new Result(true,MessageConstant.QUERY_CHECKITEM_SUCCESS,rolesId);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.QUERY_SETMEAL_FAIL);
        }
    }


    //提交修改后的数据
    @RequestMapping("/updateUserAndRole")
    public Result updateUserAndRole(@RequestBody User user,Integer[] roleId){

        try {
            userMessageService.updateUserAndRole(user,roleId);
            return new Result(true,"修改用户信息成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"修改用户信息失败");
        }
    }

    //删除用户信息
    @RequestMapping("/deleteUserAndRole")
    public Result deleteUserAndRole(Integer userId){

        try {
            userMessageService.deleteUserAndRole(userId);
            return new Result(true,"删除用户信息成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"删除用户信息失败");
        }

    }


    //根据username查询此账户是否被占用
    @RequestMapping("/findUsername")
    public Result findUsername(String username){

        long count = 0;
        try {
            count = userMessageService.findUsername(username);
            if (count>0){
                return new Result(true,"此账户已被占用");
            }else {
                return new Result(false,"此账户可用");
            }

        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"查询失败");
        }

    }



}
